#include <cstdio>
#include <iostream>
#include <ctime>
#include "Utility/GetWeather.h"
#include "Utility/pushplsh.h"
#include "Entity/MailContent.h"
#include "Entity/Receiver.h"
#include "Entity/MailAll.h"
#include "Lib/mailio/include/mailio/message.hpp"
#include "Lib/mailio/include/mailio/mime.hpp"
#include "Lib/mailio/include/mailio/smtp.hpp"

using mailio::message;
using mailio::mail_address;
using mailio::mime;
using mailio::smtp;
using mailio::smtps;
using mailio::smtp_error;
using mailio::dialog_error;
using namespace std;

int main(int argc, char *argv[]) {
    ifstream input_conf{static_cast<string>(argv[1])};
    json conf;
    input_conf >> conf;
    vector<MailAll> mailAlls;
    Sender sender{conf["sender"]["name"], conf["sender"]["email"]};
    for (auto JsonSon: conf["receiver"]) {
        Receiver receiver{JsonSon["name"], JsonSon["email"], JsonSon["location"]};
        MailAll mailAll{sender, receiver, conf["type"]};
        mailAlls.push_back(mailAll);
    }
    for (MailAll mailAll: mailAlls) {
        if (mailAll.mailContent.res_code == 200) {
            char tmp[64];
            time_t t = time(NULL);
            tm *_tm = localtime(&t);
            int year = _tm->tm_year + 1900;
            int month = _tm->tm_mon + 1;
            int date = _tm->tm_mday;
            int hh = _tm->tm_hour;
            int mm = _tm->tm_min;
            int ss = _tm->tm_sec;
            sprintf(tmp, "%04d-%02d-%02d-%02d-%02d-%02d", year, month, date, hh, mm, ss);
            try {
                message msg;
                msg.content_transfer_encoding(mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
                msg.content_type(message::media_type_t::TEXT, "plain", "utf-8");
                msg.from(mail_address(mailAll.sender.name,
                                      mailAll.sender.email));// set the correct sender name and address
                msg.add_recipient(
                        mail_address(mailAll.receiver.name,
                                /*"864969139@qq.com"*/
                                     mailAll.receiver.email));// set the correct recipent name and address
                msg.subject(mailAll.mailContent.topic);
                msg.content(mailAll.mailContent.content);
                smtps conn(conf["smtps"]["conn"]["hostname"], conf["smtps"]["conn"]["port"]);
                conn.authenticate(conf["smtps"]["authenticate"]["username"], conf["smtps"]["authenticate"]["password"],
                                  smtps::auth_method_t::LOGIN);
                conn.submit(msg);
                cout << tmp << endl << mailAll.receiver.name << ":发送成功" << endl;
            }
            catch (smtp_error &exc) {
                cout << exc.what() << endl;
            }
            catch (dialog_error &exc) {
                cout << exc.what() << endl;
            }
        }
        else if (mailAll.mailContent.res_code != -1) {
            cout << "网络出错" << mailAll.mailContent.res_code << endl;
        }
    }
    return EXIT_SUCCESS;
}